import Qt 4.7

// Main window for the example.
Rectangle{
    id: main
    width:  360
    height: 640
    color: "olive"

    // Label to set the current time.
    Text{
        id:systemTime
        text: "Time :" + alarmServer.getCurrentTime()
        color: "white"
        font.bold: true
    }

    // Label to set the current date.
    Text{
        id:systemDate
        anchors.left: systemTime.right
        anchors.leftMargin: 10
        text: "Date :" + alarmServer.getCurrentDate()
        color: "white"
        font.bold: true
    }

    // Label to set the title text for the list view.
    Text{
        id:titleText
        anchors.top: systemTime.bottom
        anchors.topMargin: 10
        text: "Added Alarms :"
        color: "white"
        font.bold: true
    }

    // A time is run after every 2 sec, to update the time and date.
    Timer {
        id: timer
        interval: 2000; repeat: true
        running: true
        triggeredOnStart: true

        onTriggered: {
            systemTime.text = "Time :" + alarmServer.getCurrentTime();
            systemDate.text = "Date :" + alarmServer.getCurrentDate();
        }
    }

    // List view of all the alarms added.
    ListView {
        id: myView
        anchors.top: titleText.bottom
        anchors.topMargin: 10
        /*
        For landscape and portrait views.
        Portrait view:
        width: parent.width (i.e 360)
        height: 640-250 = 390 (if 360 X 640)
        Landscape view: width - 310, height : 240
        */
        width: parent.width > parent.height ? 310 : parent.width
        height: parent.width > parent.height ? 240 : parent.height-250
        ListModel { id: listModel }
        model: listModel
        delegate:
            Rectangle{
            width: 200
            height: 50
            color: "grey"
            border.color:"black"

            // List element is 'Id' of the alarm.
            Text {
                text: alarmId
                anchors.centerIn: parent
                color: "white"
            }

            MouseArea{
                anchors.fill: parent
                onClicked: {
                    alarmDetailsTxt.text = alarmServer.getAlarmDetails(alarmId);
                    alarmDetailsTxt.visible = true;
                }
           }
        }
    }


    // Options to add fixed or floating alarm.
    Column{
        id: alarmOptions

        width: parent.width
        height: 170
        x:parent.width > parent.height ? parent.width-320 : 30
        y:parent.width > parent.height ? 100 : parent.height-170
        spacing: 5

        Button{ id: floatAlarm; width: 300; height: 50; operation: "Add floating alarm" ; color: "steelblue"}
        Button{ id: fixedAlarm; width: 300; height: 50; operation: "Add fixed alarm" ; color: "steelblue"}
        Button{ id: exit; width: 300; height: 50;  operation: "Exit" ; color: "firebrick"}
    }

    AlarmDetails{id: alarmDetailsTxt; width:parent.width; height:parent.height; visible: false; anchors.centerIn: main}

    // Receive signals from Alarm Server
    Connections {
        target: alarmServer
        onAlarmAdded: {
            listModel.append({'alarmId': value})
        }
    }
}
